<?php
/*
 * Chat Class
 * 
 * Lets multiple user chat through an ajax / php based chat system
 * 
 * @author : Jason Meulenhoff
 * @mail : mail@kacangboi.com
 * @web : http://kacangboi.com
 * @copyright : KacangNetworks 2010
 * 
 */
class chat
{	
	private $data = array(); 
	private $database;
	
	public function __construct()
	{
		$this->database = new databaseManager();
		if(!isset($_SESSION['CHAT']))
			$_SESSION['CHAT'] = time();
		if(!isset($_SESSION['messages']))
		{
			$_SESSION['messages'] = array();
			$_SESSION['count'] = 0;
		}
	}
	
	public function message($message,$author)
	{
		if($_SESSION['CHAT'] + TIME_TO_CHAT < time())
		{
			$this->database->saveChat($message,$author,time());
			$_SESSION['CHAT'] = time();
		}
		else
			echo '100100100';
	}
	
	public function readAllMessages()
	{
		$messages = $this->database->readAllMessages();
		$_SESSION['count'] = sizeof($messages);
		$mess = null;
		for($i = 0; $i < sizeof($messages); $i++)
		{	
			if(strtolower($messages[$i]['author']) == 'admin' || strtolower($messages[$i]['author']) == 'administrator')
					$mess .= "<span>[".date('H:i',$messages[$i]['time'])."]</span><span> [<span style='color : ".COLOR_ADMIN."';>".$messages[$i]['author']."</span>]</span><span> ".$messages[$i]['message']."</span><br/>"; 
			elseif(strtolower($messages[$i]['author']) == 'host' )
					$mess .= "<span>[".date('H:i',$messages[$i]['time'])."] </span> <span>[<span style='color : ".COLOR_HOST."';>".$messages[$i]['author']."</span>] </span> <span>".$messages[$i]['message']."</span><br/>"; 
			else
					$mess .= "<span>[".date('H:i',$messages[$i]['time'])."] </span> <span>[<span style='color : ".COLOR_USER."';>".$messages[$i]['author']."</span>] </span> <span>".$messages[$i]['message']."</span><br/>"; 
			array_push($_SESSION['messages'],$messages[$i]['time']);	
		}
		return $mess;
	}
	
	public function readNewMessages()
	{
		$messages = $this->database->readAllMessages();
		$_SESSION['count'] = sizeof($messages);
		for($i = 0; $i < sizeof($messages); $i++)
		{	
			if(!in_array($messages[$i]['time'],$_SESSION['messages']))
			{
				if(strtolower($messages[$i]['author']) == 'admin' || strtolower($messages[$i]['author']) == 'administrator')
					echo "<span>[".date('H:i',$messages[$i]['time'])."]</span><span> [<span style='color : ".COLOR_ADMIN."';>".$messages[$i]['author']."</span>]</span><span> ".$messages[$i]['message']."</span>"; 
				elseif(strtolower($messages[$i]['author']) == 'host' )
					echo "<span>[".date('H:i',$messages[$i]['time'])."] </span> <span>[<span style='color : ".COLOR_HOST."';>".$messages[$i]['author']."</span>] </span> <span>".$messages[$i]['message']."</span>"; 
				else
					echo "<span>[".date('H:i',$messages[$i]['time'])."]</span><span> [<span style='color : ".COLOR_USER."';>".$messages[$i]['author']."</span>]</span><span> ".$messages[$i]['message']."</span>"; 
				
				echo "<br/>";
				array_push($_SESSION['messages'],$messages[$i]['time']);		
			}
		}
	}
}

?>